/*******************************************************************************
* Copyright (c) 2012 
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* zhangliang - initial API and implementation
*******************************************************************************/

#ifndef CORE_MESSAGE_H
#define CORE_MESSAGE_H

#ifdef __cplusplus 
extern "C" {
#endif

typedef enum {
  kLogic,
  kTimer,
} MsgType;

/*!
 * @brief
 *  core message protocol
 *  this struct is exposed as part of the api interface
 * @note
 *  len is actually a combination of allocflag|msgtypeflag|msglen
 *  allocflag|msgtypeflag is called 'type'
 */
struct CoreMessage {
  // handle of the sending service
  uint32_t source_handle;
  // session id
  uint32_t session;       
  // data block
  const char* data;          
  // length and flags
  uint32_t data_info;                   
};

/*!
 * @brief
 *  create a core message
   * @param source_service
   *  the service which is sending the message
   *  if service is null, this is a message send by core
   * @param session
   *  session id, usually used with response message
   *  default(-1) means allocate a new session
   * @param is_owner
   *  if CoreMessage is responsible for the memory allocated for data
   * @param type
   *  message type
   * @param data
   *  message buffer
   * @param len
   *  length of message buffer
 */
bool create_message(void* source_service, 
                    uint32_t session,
                    bool is_owner,
                    MsgType type,
                    const char* data,
                    uint16_t len,
                    CoreMessage* msg);

bool message_is_owner(const CoreMessage* message);

uint8_t message_type(const CoreMessage* message);

uint16_t message_length(const CoreMessage* message);

#ifdef __cplusplus
}
#endif

#endif // CORE_MESSAGE_H

